package 蓝桥算法.A01;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.chrono.ChronoPeriod;
import java.util.Date;
import java.util.Locale;

public class a01 {
    public static void main(String[] args) throws ParseException {
        //1.十进制转化为二进制字符类型
        String str = toBinaryString(23);
        System.out.println(str);
        System.out.println(Integer.toBinaryString(23));
        System.out.println("===================================");

        //2.计算出生到现在的天数
        timeCount();
        System.out.println("===================================");

        //3.计算闰年
        getLeapYear();
        System.out.println("===================================");

    }

    private static void getLeapYear() {
        LocalDate ld = LocalDate.of(200,3,1);
        boolean y = ld.isLeapYear();
        System.out.println(y);
    }

    private static String toBinaryString(int number) {
        StringBuilder sb = new StringBuilder();

        //除基取余法
        while(true){
            if (number == 0){
                break;
            }
            int remainder = number % 2; //取余
            number = number / 2;        //除2
            sb.insert(0,remainder);

        }
        return sb.toString();//变回字符串
    }

    private static void timeCount() throws ParseException {
        //jdk7

        String str = "2006-12-25";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(str);
        long birthdayTime = date.getTime();
        long nowTime = System.currentTimeMillis();
        long period  = nowTime - birthdayTime;
        System.out.println(period/1000/60/60/24/365);

        //jdk8
        LocalDateTime birthday = LocalDateTime.of(2006,12,25,0,0);
        LocalDateTime current = LocalDateTime.now();
        Duration duration = Duration.between(birthday,current);
        System.out.println(duration.getSeconds()/60/60/24);


    }
}
